% This m-file is to produce the Qz-dependence 1D fluorescing data

MATfile={%_raw.mat data
    'SMFe01_scan49_raw.mat';   % NO KCl, FeCl3 40uM + SPM
    'SMFe01_scan95_raw.mat';  % + KCl (droplet in diffusion), FeCl3 40uM + SPM
    'SMFe01_scan127_raw.mat'; % + KCl (droplet stable), FeCl3 40uM + SPM
    'SMFe01_scan30_raw.mat';  % Pure Water Subhpase
    'SMFe01_scan35_raw.mat';  % Pure Water Subphase
    
    'SMFe01_scan44_raw.mat'; % Only Fe, No KCl, Before Reflec
    'SMFe01_scan49_raw.mat'; % Only Fe, No KCl, Before Reflec
    'SMFe01_scan76_raw.mat'; % Only Fe, No KCl, after Reflec
    'SMFe01_scan81_raw.mat'; % Only Fe, No KCl, after Reflec
    
    'SMFe01_scan175_raw.mat';   %100 mM KCl, with Fe, before relfec
    'SMFe01_scan180_raw.mat';   %100 mM KCl, with Fe, before relfec
    'SMFe01_scan207_raw.mat';   % 100 mM KCl, with Fe, after relfec
    'SMFe01_scan212_raw.mat';  % 100 mM KCl, with Fe, after relfec
    
    'SMFe01_scan221_raw.mat'; % 50 mM KCl, with Fe, before reflec
    'SMFe01_scan226_raw.mat'; % 50 mM KCl, with Fe, before reflec
    'SMFe01_scan253_raw.mat'; % 50 mM KCl, with Fe, after reflec
    'SMFe01_scan258_raw.mat'; % 50 mM KCl, with Fe, after reflec
    
    'SMFe01_scan267_raw.mat'; % 10 mM KCl, with Fe, after reflec
    'SMFe01_scan272_raw.mat'; % 10 mM KCl, with Fe, after reflec
    'SMFe01_scan299_raw.mat'; % 10 mM KCl, with Fe, after reflec
    'SMFe01_scan304_raw.mat'; % 10 mM KCl, with Fe, after reflec
    
    'SMFe01_scan313_raw.mat'; % 10 mM KCl, with Fe, after reflec
    'SMFe01_scan318_raw.mat'; % 10 mM KCl, with Fe, after reflec
    'SMFe01_scan345_raw.mat'; % 10 mM KCl, with Fe, after reflec
    'SMFe01_scan350_raw.mat'; % 10 mM KCl, with Fe, after reflec
    
    'SMFe01_scan451_raw.mat'; %  4 mM Fe, No KCl, pure subphase
    'SMFe01_scan456_raw.mat'; %  4 mM Fe, No KCl, pure subphase
    'SMFe01_scan461_raw.mat'; %  4 mM Fe, No KCl, pure subphase
    'SMFe01_scan466_raw.mat'; %  4 mM Fe, No KCl, pure subphase
    'SMFe01_scan471_raw.mat'; %  4 mM Fe, No KCl, pure subphase
    'SMFe01_scan476_raw.mat'; %  4 mM Fe, No KCl, pure subphase
   
   
    }

Floures_Fe_Qz_file={ % save dat_scan#_Fe_EL.txt (emissionline data as Qz function
        
    'SMFe01_scan49_Fe_EL.txt';   % NO KCl, FeCl3 40uM + SPM
    'SMFe01_scan95_Fe_EL.txt';  % + KCl (droplet in diffusion), FeCl3 40uM + SPM
    'SMFe01_scan127_Fe_EL.txt'; % + KCl (droplet stable), FeCl3 40uM + SPM
    'SMFe01_scan30_Fe_EL.txt';  % Pure Water Subhpase
    'SMFe01_scan35_Fe_EL.txt';  % Pure Water Subphase
    
    'SMFe01_scan44_Fe_EL.txt'; % Only Fe, No KCl, Before Reflec
    'SMFe01_scan49_Fe_EL.txt'; % Only Fe, No KCl, Before Reflec
    'SMFe01_scan76_Fe_EL.txt'; % Only Fe, No KCl, after Reflec
    'SMFe01_scan81_Fe_EL.txt'; % Only Fe, No KCl, after Reflec
    
    'SMFe01_scan175_Fe_EL.txt';   %100 mM KCl, with Fe, before relfec
    'SMFe01_scan180_Fe_EL.txt';   %100 mM KCl, with Fe, before relfec
    'SMFe01_scan207_Fe_EL.txt';   % 100 mM KCl, with Fe, after relfec
    'SMFe01_scan212_Fe_EL.txt';  % 100 mM KCl, with Fe, after relfec
    
    'SMFe01_scan221_Fe_EL.txt'; % 50 mM KCl, with Fe, before reflec
    'SMFe01_scan226_Fe_EL.txt'; % 50 mM KCl, with Fe, before reflec
    'SMFe01_scan253_Fe_EL.txt'; % 50 mM KCl, with Fe, after reflec
    'SMFe01_scan258_Fe_EL.txt'; % 50 mM KCl, with Fe, after reflec
    
    'SMFe01_scan267_Fe_EL.txt'; % 10 mM KCl, with Fe, after reflec
    'SMFe01_scan272_Fe_EL.txt'; % 10 mM KCl, with Fe, after reflec
    'SMFe01_scan299_Fe_EL.txt'; % 10 mM KCl, with Fe, after reflec
    'SMFe01_scan304_Fe_EL.txt'; % 10 mM KCl, with Fe, after reflec
    
    'SMFe01_scan313_Fe_EL.txt'; % 10 mM KCl, with Fe, after reflec
    'SMFe01_scan318_Fe_EL.txt'; % 10 mM KCl, with Fe, after reflec
    'SMFe01_scan345_Fe_EL.txt'; % 10 mM KCl, with Fe, after reflec
    'SMFe01_scan350_Fe_EL.txt'; % 10 mM KCl, with Fe, after reflec
    
    'SMFe01_scan451_Fe_EL.txt'; %  4 mM Fe, No KCl, pure subphase
    'SMFe01_scan456_Fe_EL.txt'; %  4 mM Fe, No KCl, pure subphase
    'SMFe01_scan461_Fe_EL.txt'; %  4 mM Fe, No KCl, pure subphase
    'SMFe01_scan466_Fe_EL.txt'; %  4 mM Fe, No KCl, pure subphase
    'SMFe01_scan471_Fe_EL.txt'; %  4 mM Fe, No KCl, pure subphase
    'SMFe01_scan476_Fe_EL.txt'; %  4 mM Fe, No KCl, pure subphase
    
    }


Foures_DB_Qz_file={% save dat_scan#_DB_EL (direct beam intensity) data as Qz function
    'SMFe01_scan49_DB_EL.txt';   % NO KCl, FeCl3 40uM + SPM
    'SMFe01_scan95_DB_EL.txt';  % + KCl (droplet in diffusion), FeCl3 40uM + SPM
    'SMFe01_scan127_DB_EL.txt'; % + KCl (droplet stable), FeCl3 40uM + SPM
    'SMFe01_scan30_DB_EL.txt';  % Pure Water Subhpase
    'SMFe01_scan35_DB_EL.txt';  % Pure Water Subphase
    
    'SMFe01_scan44_DB_EL.txt'; % Only Fe, No KCl, Before Reflec
    'SMFe01_scan49_DB_EL.txt'; % Only Fe, No KCl, Before Reflec
    'SMFe01_scan76_DB_EL.txt'; % Only Fe, No KCl, after Reflec
    'SMFe01_scan81_DB_EL.txt'; % Only Fe, No KCl, after Reflec
    
    'SMFe01_scan175_DB_EL.txt';   %100 mM KCl, with Fe, before relfec
    'SMFe01_scan180_DB_EL.txt';   %100 mM KCl, with Fe, before relfec
    'SMFe01_scan207_DB_EL.txt';   % 100 mM KCl, with Fe, after relfec
    'SMFe01_scan212_DB_EL.txt';  % 100 mM KCl, with Fe, after relfec
    
    'SMFe01_scan221_DB_EL.txt'; % 50 mM KCl, with Fe, before reflec
    'SMFe01_scan226_DB_EL.txt'; % 50 mM KCl, with Fe, before reflec
    'SMFe01_scan253_DB_EL.txt'; % 50 mM KCl, with Fe, after reflec
    'SMFe01_scan258_DB_EL.txt'; % 50 mM KCl, with Fe, after reflec
    
    'SMFe01_scan267_DB_EL.txt'; % 10 mM KCl, with Fe, after reflec
    'SMFe01_scan272_DB_EL.txt'; % 10 mM KCl, with Fe, after reflec
    'SMFe01_scan299_DB_EL.txt'; % 10 mM KCl, with Fe, after reflec
    'SMFe01_scan304_DB_EL.txt'; % 10 mM KCl, with Fe, after reflec
    
    'SMFe01_scan313_DB_EL.txt'; % 1 mM KCl, with Fe, after reflec
    'SMFe01_scan318_DB_EL.txt'; % 1 mM KCl, with Fe, after reflec
    'SMFe01_scan345_DB_EL.txt'; % 1 mM KCl, with Fe, after reflec
    'SMFe01_scan350_DB_EL.txt'; % 1 mM KCl, with Fe, after reflec
    
    'SMFe01_scan451_DB_EL.txt'; %  4 mM Fe, No KCl, pure subphase
    'SMFe01_scan456_DB_EL.txt'; %  4 mM Fe, No KCl, pure subphase
    'SMFe01_scan461_DB_EL.txt'; %  4 mM Fe, No KCl, pure subphase
    'SMFe01_scan466_DB_EL.txt'; %  4 mM Fe, No KCl, pure subphase
    'SMFe01_scan471_DB_EL.txt'; %  4 mM Fe, No KCl, pure subphase
    'SMFe01_scan476_DB_EL.txt'; %  4 mM Fe, No KCl, pure subphase
    }


ch=[1:2048];
CalibCoef=[0.0119,-0.0972];  % Channel->Energy calibration coefficients Ames detector
E=ch*CalibCoef(1)+CalibCoef(2);

Target_EL={'Fe','DB'} % Target to-be-integrated emission lines
CaseNum=1;
switch Target_EL{CaseNum}
    case 'Fe'
        Emin=6.25;
        Emax=6.65;
    case 'DB'
        Emin=7.75;
        Emax=8.25;
    otherwise
        mywarndlg = warndlg('Invalid input', 'My Warn Dialog');
end

EM=1;
EN=2048;
for k=1:length(E)
    if E(k)>Emin;EM=k-1;break;
    end
end

for k=1:length(E)
    if E(k)>Emax;EN=k-1;break;
    end
end
%%
close all
figure
subplot(1,3,1)
hold on
MarkerType={'o','s','v','^','d','>','<'}
COLOR={'k','r','b',[0 0.4 0],'m','k','k'}
for k=1:length(MATfile)
    data=load(MATfile{k});
    temp=sum(data.mca(EM:EN,:),1);
    temp_err=sqrt(temp);
    mon=data.mon;
    qz=data.qz;
    % ct=data.ct;  % Ames Lab no 
    Intensity1D=temp./mon;
    Intensity1D_err=temp_err./mon;
    H=ploterr(qz,Intensity1D,[],Intensity1D_err,MarkerType{mod(k,7)+1},'hhy',0.1);
    set(H,'LineWidth',3,'color',COLOR{mod(k,7)+1});
    switch Target_EL{CaseNum}
        case 'Fe'
            Txt_Save(Floures_Fe_Qz_file{k},qz,Intensity1D,Intensity1D_err);
        case 'DB'
            Txt_Save(Foures_DB_Qz_file{k},qz,Intensity1D,Intensity1D_err);
        otherwise
            mywarndlg = warndlg('Invalid input', 'My Warn Dialog');
    end
end
hold off
set(gca,'LineWidth',3,'box','on');



        
        
        
        
        